/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.service.impl;

import io.renren.common.exception.ErrorCode;
import io.renren.common.exception.RenException;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.validator.AssertUtils;
import io.renren.dao.UserDao;
import io.renren.dto.SupplierDTO;
import io.renren.dto.SysRoleDTO;
import io.renren.entity.TokenEntity;
import io.renren.entity.UserEntity;
import io.renren.dto.LoginDTO;
import io.renren.password.PasswordUtils;
import io.renren.service.*;
import io.renren.utils.JwtUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implements UserService {


	@Autowired
	private JwtUtils jwtUtils;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysDeptService sysDeptService;
	@Override
	public UserEntity getByMobile(String mobile) {
		return baseDao.getUserByMobile(mobile);
	}

	@Override
	public UserEntity getUserByUserId(Long userId) {
		return baseDao.getUserByUserId(userId);
	}

	@Override
	public Map<String, Object> login(LoginDTO dto) {
		UserEntity user = getByMobile(dto.getUsername());
		AssertUtils.isNull(user, ErrorCode.ACCOUNT_PASSWORD_ERROR);

		//密码错误
		if(user==null || !PasswordUtils.matches(dto.getPassword(),user.getPassword())){
			throw new RenException(ErrorCode.ACCOUNT_PASSWORD_ERROR);
		}

		//生成token
		String token = jwtUtils.generateToken(user.getId());
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		map.put("userid",user.getId());
		map.put("expire", jwtUtils.getExpire());
		map.put("realName",user.getRealName());//姓名
		map.put("username", user.getUsername());//角色登录账号
		map.put("name", getRoleName(user.getId()).getName());//角色名称
		map.put("flag", getRoleName(user.getId()).getFlag());//角色标识
		map.put("canteenId",user.getDeptId());
		map.put("deptName",sysDeptService.getDeptName(user.getDeptId()).getName());//新（老）食堂
		if("1203225543123369985L".equals(user.getDeptId())){
			SupplierDTO supplier = supplierService.getSupplier(user.getId());
			if(null != supplier){
				map.put("supplierName",supplier.getSupplierName());
			}
		}
		System.out.println(map);
		return map;
	}
	//获取角色名称和角色的标识
	SysRoleDTO  getRoleName(Long userId){
		SysRoleDTO sysRoleDTO = sysRoleService.getRoleName(userId).get(0);
		Long id = sysRoleDTO.getId();
		int flag = 1;//管理员
		if (1204689284801183745L== id){
			flag =2; //主厨
		}else if (1204689511473954818L == id ){
			flag= 3; //食堂管理员
		}else if(1204689119201673218L == id){
			flag =4; //供应商
		}else if (1204689350840500226L== id){
			flag =5; //仓储员
		}else if (1204689446743261186L == id){
			flag =6; //财务
		}else if (1204689394217992193L== id){
			flag =7; //监管员
		}else if(1210451039619792898L ==id){
			flag =8; //批量采购员
		}
		sysRoleDTO.setFlag(flag);
		return sysRoleDTO;
	}
}